package com.monitor.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.monitor.commons.HttpTookit;
import com.monitor.commons.StringUtil;
import com.monitor.config.yunke.DeptAndUser;
import com.monitor.config.yunke.DeptUserResult;
import com.monitor.config.yunke.YunKeConfig;
import com.monitor.config.yunke.YunKeUtil;
import com.monitor.dao.IDeptDao;
import com.monitor.dao.IUserDao;
import com.monitor.model.Dept;
import com.monitor.model.User;
import com.monitor.modelExt.DeptExt;
import com.monitor.modelExt.UserExt;
import com.monitor.pubInter.AbstractBaseService;
import com.monitor.pubInter.IBaseDao;
import com.monitor.pubInter.exception.SkException;
import com.monitor.searchExt.DeptSearchExt;
import com.monitor.searchExt.UserSearchExt;
import com.monitor.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author afeng
 * @Date 2025-03-17 10:50:14
 * @description DeptService      部门信息
 */
@Service(value = "deptService")
@Slf4j
public class DeptServiceImpl extends AbstractBaseService<DeptExt, DeptSearchExt, String> implements IDeptService {

    @Resource(name = "deptDao")
    private IDeptDao deptDao;

    @Resource(name = "userDao")
    private IUserDao userDao;

    @Autowired
    private YunKeUtil yunKeUtil;
    @Autowired
    private YunKeConfig yunKeConfig;

    @Override
    public IBaseDao<DeptExt, DeptSearchExt, String> getDao() {
        return deptDao;
    }

    @Override
    public void fetchAndUpdateDeptAndUser() throws SkException {
        try {


            //定义请求路径
            String url = yunKeConfig.getUrl() + "open/department/deptAndUsers";
            //组装请求头
            long timestamp = new Date().getTime();
            Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
            //组装请求参数
            Map<String, Object> paramsMap = new HashMap<>();
            String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
            JSONObject resObject = JSONObject.parseObject(res);
            if (resObject.getBoolean("success")) {
                JSONObject dataObj = resObject.getJSONObject("data");
                DeptUserResult result = DeptAndUser.classifyByType(dataObj);

                List<DeptExt> deptExts = result.getDepts();
                List<DeptExt> oldDepts = deptDao.getAllList(DeptSearchExt.getInstance());
                Set<String> oldDeptIds = oldDepts.stream().map(Dept::getYkId).collect(Collectors.toSet());
                List<DeptExt> newDepts = deptExts.stream().filter(item -> !oldDeptIds.contains(item.getYkId())).collect(Collectors.toList());
                if (StringUtil.isNotObjEmpty(newDepts) && !newDepts.isEmpty()) {
                    deptDao.batchInsert(newDepts);
                }

                List<UserExt> userExts = result.getUsers();
                UserSearchExt userSearch = UserSearchExt.getInstance();
                userSearch.setGroupBy(User.ID);
                List<UserExt> oldUsers = userDao.getAllList(userSearch);
                Set<String> oldUserIds = oldUsers.stream().map(User::getYkId).collect(Collectors.toSet());
                List<UserExt> newUsers = userExts.stream().filter(item -> !oldUserIds.contains(item.getYkId())).collect(Collectors.toList());
                if (StringUtil.isNotObjEmpty(newUsers) && !newUsers.isEmpty()) {
                    userDao.batchInsert(newUsers);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}